package com.atguigu.login.dao;

import com.atguigu.login.beans.User;

import java.sql.*;

public class UserDaoImpl{
    public User getUser(String userName, String password) {
        User u = null;
        String URL = "jdbc:mysql://127.0.0.1:3306/servlet?characterEncoding=utf-8";
        String USER = "root";
        String PASSWORD = "root";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获得数据库链接
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            // 3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
            //预编译
            String sql="select userName, password  from t_admin where userName =? and password = ?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, userName);
            statement.setString(2, password);
            ResultSet rs = statement.executeQuery();
            // 4.处理数据库的返回结果(使用ResultSet类)
            if (rs.next()) {
                u = new User();
                u.setUserName(rs.getString("userName"));
                u.setPassword(rs.getString("password"));
            }
            return u;


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
